﻿using System;
using fastCSharp;
using fastCSharp.code.cSharp;

namespace diantou.web.topic
{
    /// <summary>
    /// 话题视图
    /// </summary>
    /// <typeparam name="viewType"></typeparam>
    class view<viewType> : web.view<viewType>
        where viewType : view<viewType>
    {
        /// <summary>
        /// 最佳回答
        /// </summary>
        protected struct bestAnswerTool
        {
            /// <summary>
            /// 是否重新加载视图
            /// </summary>
            [fastCSharp.code.cSharp.webView.outputAjax]
            public bool ViewOnly;
            /// <summary>
            /// 话题标识
            /// </summary>
            public int TopicId;
            /// <summary>
            /// 最佳回答
            /// </summary>
            private diantou.dataAccess.topic.bestAnswer[] top3;
            /// <summary>
            /// 最佳回答
            /// </summary>
            public diantou.dataAccess.topic.bestAnswer[] Top3
            {
                get
                {
                    if (ViewOnly) return null;
                    if (top3 == null)
                    {
                        int count;
                        top3 = diantou.dataAccess.tcpCall.topic.getBestAnswerUser(TopicId, 3, 1, out count);
                        if (top3 == null) top3 = nullValue<diantou.dataAccess.topic.bestAnswer>.Array;
                        else for (count = top3.Length; count != 0; top3[--count].Set(TopicId, count)) ;
                    }
                    return top3;
                }
            }
        }
        /// <summary>
        /// 话题
        /// </summary>
        [fastCSharp.emit.webView.clearMember]
        protected diantou.dataAccess.topic topic;
        /// <summary>
        /// 用户话题关系
        /// </summary>
        protected userTopic userTopic
        {
            get { return new userTopic { Topic = topic, UserId = currentUserId }; }
        }
        /// <summary>
        /// 关注者集合
        /// </summary>
        protected focusImage focusImage
        {
            get
            {
                if (requestHeader.IsReView) return new focusImage { ViewOnly = true };
                int count;
                return new focusImage(diantou.dataAccess.clientCache.User.Get(diantou.dataAccess.tcpCall.focusTopic.getUserIds(topic.Id, diantou.dataModel.focus.type.Focus, 4 * 10, 1, out count)), 10);
            }
        }
        /// <summary>
        /// 相关收藏top5
        /// </summary>
        protected userTopic.relateFavorite relateFavorite
        {
            get
            {
                return new userTopic.relateFavorite { ViewOnly = requestHeader.IsReView, Topic = new userTopic { Topic = topic, UserId = currentUserId } };
            }
        }
        /// <summary>
        /// 最佳回答
        /// </summary>
        protected bestAnswerTool bestAnswer
        {
            get { return new bestAnswerTool { ViewOnly = requestHeader.IsReView, TopicId = topic.Id }; }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="topicId"></param>
        /// <returns></returns>
        protected virtual bool loadView(int topicId)
        {
            return load(topicId);
        }
        /// <summary>
        /// 加载视图
        /// </summary>
        /// <param name="topicId"></param>
        /// <param name="searchEnginePath">搜索引擎重定向</param>
        /// <returns></returns>
        protected bool load(int topicId, string searchEnginePath = null)
        {
            if (topicId == 0)
            {
                checkSearchEngine(searchEnginePath, webConfig.NotFound404);
                return false;
            }
            topic = diantou.dataAccess.tcpCall.topic.Get(topicId);
            if (topic != null) return true;
            AjaxResponse(webConfig.NotFound404);
            return false;
        }
        /// <summary>
        /// 加载视图
        /// </summary>
        /// <param name="topicId"></param>
        /// <param name="searchEnginePath">搜索引擎重定向</param>
        /// <returns></returns>
        protected bool load(ref idName topicId, Func<string, webView.hashUrl> getNamePath = null, string searchEnginePath = null)
        {
            if (!topicId.IsValue)
            {
                checkSearchEngine(searchEnginePath, webConfig.NotFound404);
                return false;
            }
            if (topicId.Id == 0)
            {
                topic = diantou.dataAccess.tcpCall.topic.Get(topicId.Name);
                if (topic != null)
                {
                    topicId.Id = topic.Id;
                    return true;
                }
                if (getNamePath != null && topicId.Name.Length != 0)
                {
                    location(getNamePath(topicId.Name), false);
                    return false;
                }
            }
            else
            {
                topic = diantou.dataAccess.tcpCall.topic.Get(topicId.Id);
                if (topic != null) return true;
            }
            AjaxResponse(webConfig.NotFound404);
            return false;
        }
    }
}
